// index.js
const router = require('koa-router')()


// 登录
router.post("/login", async (ctx, next) => {
  console.log(ctx.request.body);
  let sql = 'select * from users where userName=? and password=?';
  let res = await ctx.db.EXCUTE(sql, [ctx.request.body.userName, ctx.request.body.password]);
  if (res.length > 0) {
    ctx.body = { code: 200, message: res }
  } 
  else {
    ctx.body = { code: 400, message: "fail" }
  }
})

//修改
//查看输入用户名是否已注册
router.post("/selectUser", async (ctx, next) => {
  let sql1 = 'select * from users where userName=? ';
  let res1 = await ctx.db.EXCUTE(sql1, [ctx.request.body.userName]);
  if (res1.length > 0) {
    ctx.body = { code: 200, message: "该用户名已存在" }
  } else{
    ctx.body = { code: 300, message: "该用户名未存在" }
  }
})
//输入用户名未被注册后，弹框协议进行注册
router.post("/register", async (ctx, next) => {
    let sql = 'insert into users(userName,password) values (?,?)';
    let res = await ctx.db.EXCUTE(sql, [ctx.request.body.userName, ctx.request.body.password]); 
  if (res) {
    ctx.body = { code: 200, message: "success" }
  }
  else {
    ctx.body = { code: 300, message: "fail" }
  }
})

// 忘记密码
router.post("/forget", async (ctx, next) => {
  console.log(ctx.request.body);
  let sql = 'select password from users where userName=? and birthday=?';
  let res = await ctx.db.EXCUTE(sql, [ctx.request.body.userName, ctx.request.body.birthday]);
  if (res.length > 0) {
    ctx.body = { code: 200, message: res }
  } 
  else {
    ctx.body = { code: 400, message: "fail" }
  }
})

//购物车列表
router.post('/shopList', async (ctx, next) => {
  let id = JSON.parse(ctx.request.body);
  let { userName } = id;
  let sql = 'select * from shop where shopState = "noPay" and userName=?';
  let sql1 = 'select * from shop where shopState = "shop" and userName=?';
  console.log(userName);
  let res = await ctx.db.EXCUTE(sql, [userName])
  let res1 = await ctx.db.EXCUTE(sql1, [userName])
  res = [...res, ...res1]
  ctx.body = { List: res };
})

//购物车删除功能
router.post('/shopDelete', async (ctx, next) => {
  let id = JSON.parse(ctx.request.body);
  let sql1 = 'delete from shop where shopId=?';
  let res1 = await ctx.db.EXCUTE(sql1, [id.shopId])
  if (res1.affectedRows != 0) {
    let sql2 = 'select * from shop'
    let res2 = await ctx.db.EXCUTE(sql2)
    ctx.body = { code: 200, List: res2 }
  } 
  else {
    ctx.body = { code: 400 }
  }
})

// 前端：显现所有地址信息
router.post('/DeliveryAddress', async (ctx, next) => {
  let id = JSON.parse(ctx.request.body);
  let sql = 'select * from address where userName = ?'
  let pinglunList = await ctx.db.EXCUTE(sql, [id.userName]);
  ctx.body = { List: pinglunList };
})

//支付界面获取对应收货地址信息
router.post('/addressList', async (ctx, next) => {
  let id = JSON.parse(ctx.request.body);
  let sql = 'select * from address where addressId=?';
  let res = await ctx.db.EXCUTE(sql, [id.addressId])
  if (res.length > 0) {
    ctx.body = { code: 200, List: res }
  } 
  else {
    ctx.body = { code: 400, message: 'fail' }
  }
})

//支付界面获取对应商品信息
router.post('/payList', async (ctx, next) => {
  let id = JSON.parse(ctx.request.body);
  let { shopId } = id
  let sql = 'select * from shop where shopId=?';
  let res = await ctx.db.EXCUTE(sql, [shopId])
  if (res.length > 0) {
    ctx.body = { code: 200, List: res }
  } 
  else {
    ctx.body = { code: 400, message: 'fail' }
  }
})

//取消支付后将商品状态修改为“noPay”
router.post('/cancelPay', async (ctx, next) => {
  let id = JSON.parse(ctx.request.body);
  let { shopId } = id
  console.log(id);
  let sql1 = 'select * from shop where shopId=? and shopState=?';
  let res1 = await ctx.db.EXCUTE(sql1, [id.shopId, id.shopState])
  if (res1.length > 0) {
    let sql2 = 'update shop set shopState=? where shopId=?';
    let res2 = await ctx.db.EXCUTE(sql2, ["noPay", shopId])
    ctx.body = { code: 200 }
  } 
  else {
    ctx.body = { code: 400 }
  }
})

//支付成功后将商品状态修改为"pay"
router.post('/onPay', async (ctx, next) => {
  let id = JSON.parse(ctx.request.body);
  let { shopId, shopState } = id
  let sql1 = 'select * from shop where shopId=? and shopState=?';
  let res1 = await ctx.db.EXCUTE(sql1, [id.shopId, id.shopState])
  console.log(res1);
  if (res1.length > 0) {

    let sql2 = 'update shop set shopState=? where shopId=?';
    let res2 = await ctx.db.EXCUTE(sql2, ["pay", shopId])
    ctx.body = { code: 200 }
  } 
  else {
    ctx.body = { code: 400 }
  }
})


// 支付成功后扣除支付方商品单价＋押金，收款方余额加入单价*0.8（平台盈利单价的20%）
router.post('/changeMoney', async (ctx, next) => {
  let id = JSON.parse(ctx.request.body);
  // console.log(id.shopName);
  let sql1 = 'select * from shop where shopId=? ';
  let res1 = await ctx.db.EXCUTE(sql1, [id.shopId])
  if (res1.length > 0) {
    // 扣除支付方商品单价＋押金
    let sql2 = 'select * from package where userName=? ';
    let res2= await ctx.db.EXCUTE(sql2, [id.userName])
    let sql3 = 'update package set money=? where userName=?';
    let res3 = await ctx.db.EXCUTE(sql3, [res2[0].money-30-res1[0].shopPrice, id.userName])
    // 收款方余额增加单价*0.8
    let sql4 = 'select * from package where userName=? ';
    let res4 = await ctx.db.EXCUTE(sql4, [id.shopName])
    if (res4.length>0){
      let sql5 = 'update package set money=? where userName=?';
      let res5 = await ctx.db.EXCUTE(sql5, [res4[0].money+res1[0].shopPrice*0.8, id.shopName])
    }
    else {
      let sql6 = 'insert into package (money,userName) values (?,?)';
      let res6 = await ctx.db.EXCUTE(sql6,[res1[0].shopPrice*0.8, id.shopName])
    }
    ctx.body = { code: 200 }
  } 
  else {
    ctx.body = { code: 400 }
  }
})

// 订单中点击“全部” “待付款” “已支付”"已出租”“已完成”出现对应内容
router.post('/order', async (ctx, next) => {
  let id = JSON.parse(ctx.request.body);
  let { userName } = id
  if (id.shopState == "全部") {
    let sql = 'select * from shop where shopState = "noPay" and userName=?';
    let res = await ctx.db.EXCUTE(sql, [userName])
    let sql1 = 'select * from shop where shopState = "pay" and userName=?';
    let res1 = await ctx.db.EXCUTE(sql1, [userName])
    let sql2 = 'select * from shop where shopState = "end" and userName=?';
    let res2 = await ctx.db.EXCUTE(sql2, [userName])
    res = [...res, ...res1,...res2]
    let Asql1 = 'select * from shop where shopState = "pay" and shopName=?';
    let Ares1 = await ctx.db.EXCUTE(Asql1, [userName])
    res = [...res, ...Ares1]
    ctx.body = { List: res };
  } 
  else if (id.shopState == "待付款") {
    let sql = 'select * from shop where shopState = "noPay" and userName=?';
    let res = await ctx.db.EXCUTE(sql, [userName])
    console.log(res);
    ctx.body = { List: res };
  } 
  else if (id.shopState == "已支付") {
    let sql = 'select * from shop where shopState = "pay" and userName=?';
    let res = await ctx.db.EXCUTE(sql, [userName])
    console.log(res);
    ctx.body = { List: res };
  } 
  else if (id.shopState == "已完成") {
    let sql = 'select * from shop where shopState = "end" and userName=?';
    let res = await ctx.db.EXCUTE(sql, [userName])
    console.log(res);
    ctx.body = { List: res };
  } 
  else {
    let sql = 'select * from shop where shopState = "pay" and shopName=?';
    let res = await ctx.db.EXCUTE(sql, [userName])
    console.log(res);
    ctx.body = { List: res };
  }

})


//支付成功后将逾期押金修改为15
router.post('/over', async (ctx, next) => {
  let id = JSON.parse(ctx.request.body);
  let sql1 = 'select * from shop where shopId=?';
  let res1 = await ctx.db.EXCUTE(sql1, [id.shopId])
  console.log(res1);
  if (res1.length > 0) {
    let sql2 = 'update shop set  ifDue="逾期" where shopId=?';
    let res2 = await ctx.db.EXCUTE(sql2, [id.shopId])
    let sql3 = 'update shop set  overdueDeposit="15" where shopId=?';
    let res3 = await ctx.db.EXCUTE(sql3, [id.shopId])
    // console.log("aaaaaaaaaaaaaaa");
    // console.log(res2);
    ctx.body = { code: 200}
  } 
  else {
    ctx.body = { code: 400 }
  }
})

// 前端：显现余额
router.post('/Package', async (ctx, next) => {
  let id = JSON.parse(ctx.request.body)
  let sql = 'select * from package where userName=?'
  let List = await ctx.db.EXCUTE(sql,[id.userName]);
  console.log(List)
  ctx.body = { List: List };
})

router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})

module.exports = router
